
/**
 *
 * 双击转向
 *
 * @param    viewer  Mars3d实例对象
 * @param    range   飞行终点距目标点距离
 * @param    start   开启转向事件
 * @param    stop    停止转向事件
 * @param    destroy 卸载转向事件
 * 
 * @returns  BQGIS_cameraDblClickMove
 *
 */

export function BQGIS_initCameraDblClickMove(viewer = window.viewer, range = 200) {
    return new BQGIS_cameraDblClickMove(viewer, range);
}


function BQGIS_cameraDblClickMove (
    viewer = window.viewer,
    range = 200,
){
    this.viewer = viewer
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    this.range = range
    this.earthPosition = null
}

BQGIS_cameraDblClickMove.prototype.start = function () {
    if(this.handler.isDestroyed()){
        this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
    }
    this.handler.setInputAction((event) => {
        this.viewer.scene.globe.depthTestAgainstTerrain = true;
        const feature = this.viewer.scene.pick(event.position)
        if(feature){
            this.earthPosition = this.viewer.scene.pickPosition(event.position)
        }else{
            this.viewer.scene.globe.depthTestAgainstTerrain = false;
            this.earthPosition  = this.viewer.camera.pickEllipsoid(event.position,this.viewer.scene.globe.ellipsoid);
        }
        if(!this.earthPosition) return
        const bounding =  new Cesium.BoundingSphere(this.earthPosition, this.range)
        const heading = this.viewer.camera.heading
        const pitch = this.viewer.camera.pitch
        this.viewer.camera.flyToBoundingSphere (bounding, {
          offset: new Cesium.HeadingPitchRange(heading, pitch, this.range)
        })
      }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
}

BQGIS_cameraDblClickMove.prototype.stop = function () {
    this.handler.destroy()
}

BQGIS_cameraDblClickMove.prototype.destroy = function () {
    this.viewer = null
    this.handler.destroy()
    this.handler = null
    this.range = null
    this.ignoringModel = null
    this.earthPosition = null
}